/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-10-06 21:08:50
 * Last Modified: 2017-04-27 17:16:12
 * File Name: controllers/washer.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"qkleague/errorcode"
	rpc "qkleague/rpc/client/qingke"
	//"qkleague/statis"

	log "github.com/cihub/seelog"
)

type WasherController struct {
	LoginedController
}

/*
	添加洗衣机
*/
// @router /add [post]
func (p *WasherController) AddWahserInfo() {
	defer p.Output("AddWahserInfo")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["washer_id"] = "test1300000000"
		return
	}

	body := p.Ctx.Input.RequestBody
	log.Infof("receve body, %s", string(body))

	washer := make(map[string]interface{})
	if err := json.Unmarshal(body, &washer); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	washer["admin_uid"] = p.Userid
	washer["deploy_type"] = 2

	if ok, washerId, msg := rpc.AddWasher(p.Userid, washer); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["washer_id"] = washerId

		/*extra := map[string]string{
			"washer_id":   washerId,
			"room_id":     washer["room_id"].(string),
			"washer_addr": washer["addr_detail"].(string),
		}
		statis.GetQKleagueStatis().AddMemData(1, extra)*/
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = msg
	}
}

/*
	修改洗衣机信息
*/
// @router /modify [post]
func (p *WasherController) ModifyWahserInfo() {
	defer p.Output("ModifyWahserInfo")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		return
	}

	body := p.Ctx.Input.RequestBody

	log.Infof("receve body, %s", string(body))
	washer := make(map[string]interface{})
	if err := json.Unmarshal(body, &washer); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code, msg := rpc.ModifyWasher(washer)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

/*
	查询3种状态下的洗衣机数量
*/
// @router /amount [get]
func (p *WasherController) QueryWahserStatusAmount() {
	defer p.Output("QueryWahserStatus")

	if ok, data := rpc.QueryKindWashers(p.Userid); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	新版-查询3种状态下的洗衣机数量
*/
// @router /amount/new [get]
func (p *WasherController) NewQueryWahserStatusAmount() {
	defer p.Output("NewQueryWahserStatusAmount")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/washer-amount.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	if ok, data := rpc.NewQueryKindWashers(p.Userid); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	获取所有洗衣机信息
*/
// @router /all [get]
func (p *WasherController) QueryAllWashers() {
	defer p.Output("QueryAllWashers")

	if data := rpc.GetAllWashers(p.Userid); data != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	新版-获取所有洗衣机信息
*/
// @router /all/new [get]
func (p *WasherController) NewQueryAllWashers() {
	defer p.Output("NewQueryAllWashers")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/washer-all.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	if data := rpc.NewGetAllWashers(p.Userid); data != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
		log.Debugf("NewQueryAllWashers, %s\n", data)
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	根据洗衣机状态获取洗衣机详情
*/
// @router /list/bystatus [get]
func (p *WasherController) QueryWahserInfoByStatus() {
	defer p.Output("QueryWahserInfoByStatus")

	status, _ := p.GetInt("status", 0)

	if ok, data := rpc.QueryWahserInfoByStatus(p.Userid, status); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	新版-根据洗衣机状态获取洗衣机详情
*/
// @router /list/bystatus/new [get]
func (p *WasherController) NewQueryWahserInfoByStatus() {
	defer p.Output("NewQueryWahserInfoByStatus")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/washer-list-bystatus.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	status, _ := p.GetInt("status", 0)

	if ok, data := rpc.NewQueryWahserInfoByStatus(p.Userid, status); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	根据洗衣房获取洗衣机详情
*/
// @router /list/byroom [get]
func (p *WasherController) QueryWahserInfoByRoom() {
	defer p.Output("QueryWahserInfoByRoom")

	roomId := p.GetString("room_id", "")
	if ok, data := rpc.QueryWahserInfoByRoomid(p.Userid, roomId); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	新版-根据洗衣房获取洗衣机详情
*/
// @router /list/byroom/new [get]
func (p *WasherController) NewQueryWahserInfoByRoom() {
	defer p.Output("NewQueryWahserInfoByRoom")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/washer-list-byroom.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	roomId := p.GetString("room_id", "")
	if ok, data := rpc.NewQueryWahserInfoByRoomid(p.Userid, roomId); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

//停启用洗衣机
// @router /enable/status [post]
func (p *WasherController) EnableWasher() {
	defer p.Output("EnableWasher")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["run_state"] = map[string]interface{}{
			"ws_show_code":  1,
			"ws_show_msg":   "空闲",
			"ws_detail_msg": "等待使用",
		}
		return
	}

	body := p.Ctx.Input.RequestBody
	req := make(map[string]interface{})
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	_, ok := req["washer_id"]
	_, ok2 := req["enable"]
	if !ok || !ok2 {
		log.Debugf("EnableWasher, req[\"washer_id\"]:%v, req[\"enable\"]:%v", ok, ok2)
		p.Resp["code"] = errorcode.EC_WASHER_ENABLE_PARAM_ERR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_WASHER_ENABLE_PARAM_ERR)
		return
	}

	if data := rpc.EnableWasher(p.Userid, req); data != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["run_state"] = data
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

//定位洗衣机
// @router /locate [post]
func (p *WasherController) WasherLocate() {
	defer p.Output("WasherLocate")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		return
	}
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body: %s", string(body))
	var req struct {
		WasherId string `json:"washer_id"`
	}
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	if req.WasherId != "" {
		if ok := rpc.LocateWasher(req.WasherId); ok {
			p.Resp["code"] = errorcode.EC_GL_SUCCESS
			p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		} else {
			p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
			p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
		}
	} else {
		p.Resp["code"] = errorcode.EC_WASHER_WASHER_LOCATE_WASHER_ID_INVALID
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_WASHER_WASHER_LOCATE_WASHER_ID_INVALID)
	}
}
